package com.tyjc.unique.dlt;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.tyjc.unique.bean.DLTTrendChart;
import com.tyjc.unique.net.StringResultBean;
import com.tyjc.unique.net.UrlConstants;
import com.tyjc.unique.ssq._AnalaysesBaseFragment;
import com.tyjc.unique.util.DLT_ZhuShu;
import com.fyzk.activity.lottery.shuzicai.BallInfo;
import com.fyzk.activity.lottery.shuzicai.SelectBallView;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.tyjc.lottery.R;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;

import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;

/**
 * Created by miao on 16/6/12.
 */
@ContentView(R.layout.fg_dltcoldhot)
public class DltColdHotFragment extends _AnalaysesBaseFragment implements TouZhuHelper
        .BallClickListener, TouZhuHelper.BallItemClickLitener {

    @ViewInject(R.id.cold_30)
    AppCompatTextView cold_30;

    @ViewInject(R.id.cold_50)
    AppCompatTextView cold_50;

    @ViewInject(R.id.cold_100)
    AppCompatTextView cold_100;

    @ViewInject(R.id.hot_30)
    AppCompatTextView hot_30;

    @ViewInject(R.id.hot_50)
    AppCompatTextView hot_50;

    @ViewInject(R.id.hot_100)
    AppCompatTextView hot_100;

    @ViewInject(R.id.firstLine)
    SelectBallView firstLine;

    @ViewInject(R.id.twoLine)
    SelectBallView twoLine;

    @ViewInject(R.id.tv_zhushu)
    TextView tv_zhushu;

    @ViewInject(R.id.tv_money)
    TextView tv_money;

    @ViewInject(R.id.btn_bet)
    Button btn_bet;

    private ArrayList<DLTTrendChart> arrayChartItems;
    ProgressDialog progressDialog;


    long startTime = 0L;
    long endTime = 0L;

    private int hong_count = 35;
    private int lan_count = 12;

    int zhushu;

    List<BallInfo> balls_firstLine = new ArrayList<BallInfo>();
    List<BallInfo> balls_twoLine = new ArrayList<BallInfo>();
    protected List<List<BallInfo>> arr_infos = new ArrayList<List<BallInfo>>();

    @Override
    public void showData() {
        super.showData();
        TouZhuHelper.getInstance().setBallClickListener(this);
        TouZhuHelper.getInstance().setBallItemClickLitener(this);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initBallList(balls_firstLine, hong_count, 1, true);
        initBallList(balls_twoLine, lan_count, 1, true);
        arr_infos.add(balls_firstLine);
        arr_infos.add(balls_twoLine);
        firstLine.setBallInfoList(balls_firstLine);
        firstLine.setSelect_max(18);
        twoLine.setBallInfoList(balls_twoLine);
        btn_bet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(zhushu>0){
                    dltBet(arr_infos, zhushu);
                }else {
                    showToast("至少选择一注");
                }
            }
        });
        if(MySharedPreferences.getInstance().getOpenStatus()){
            btn_bet.setVisibility(View.VISIBLE);
        }else{
            btn_bet.setVisibility(View.GONE);
        }
    }

    final long waittime = 2000;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {

        if (resultBean.getStatus() == 0) {
//            endTime = System.currentimeMiis();
            arrayChartItems = (ArrayList<DLTTrendChart>) JSON.parseArray(
                    resultBean.getData(), DLTTrendChart.class);
            int phase = Integer.parseInt(arrayChartItems.get(arrayChartItems.size() - 1).getPhase())
                    + 1;
            if (TextUtil.isEmpty(AppLotteryInfos.getPeriod(LotteryIDUtils.DLT_LOTTERY_ID_INT))) {
                AppLotteryInfos.setPeriod(LotteryIDUtils.DLT_LOTTERY_ID_INT, phase + "");
            }
//            if (endTime - startTime > waittime) {
            showColdHotNum();
//            } else {
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        showColdHotNum();
//                    }
//                }, (waittime - endTime + startTime));
//            }

        }
    }

    @Override
    public void onStart() {
        super.onStart();
//        if(getContext() != null){
//            progressDialog = new ProgressDialog(getContext(),
//                    R.style.Custom_Progress);
//            progressDialog.setIndeterminate(true);
//            progressDialog.setCancelable(true);
//            progressDialog.setMessage("冷热号码分析中...");
        startTime = System.currentTimeMillis();
//            progressDialog.show();
        requestChart();
//        }
        com.fyzk.utils.LogUtil.v("onStart>>>>>>>>>>>>>>>>");
    }

    @Override
    public void onResume() {
        super.onResume();
        com.fyzk.utils.LogUtil.v("onResume>>>>>>>>>>>>>>>>");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        com.fyzk.utils.LogUtil.v("onDetach>>>>>>>>>>>>>>>>");
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        com.fyzk.utils.LogUtil.v("onAttach context>>>>>>>>>>>>>>>>");

    }

    @Override
    public void onPause() {
        super.onPause();
        com.fyzk.utils.LogUtil.v("onPause >>>>>>>>>>>>>>>>");
    }

    private void showColdHotNum() {

//        progressDialog.dismiss();

        coldhotCompute(0);
        hot_100.setText(Html.fromHtml("热号：红球 <font color=\"#eb4f38\"><b>" + lowNinePlusZero
                (min_1_index) + "," + lowNinePlusZero(min_2_index) + "," + lowNinePlusZero
                (min_3_index) + "," + lowNinePlusZero
                (min_4_index) + "</b></font>| 篮球 <font color=\"#56abe4\"><b>" + lowNinePlusZero
                (min_1_blue_index) + "," + lowNinePlusZero
                (min_2_blue_index) + "</b></font>"));
        cold_100.setText(Html.fromHtml("冷号：红球 <font color=\"#eb4f38\"> <b>" + lowNinePlusZero
                (max_1_index) + "," + lowNinePlusZero(max_2_index) + "," + lowNinePlusZero
                (max_3_index) + "," + lowNinePlusZero
                (max_4_index) + "</b></font>| 篮球 <font color=\"#56abe4\"> <b>" + lowNinePlusZero
                (max_1_blue_index) + "," + lowNinePlusZero
                (max_2_blue_index) + "</b></font>"));

        coldhotCompute(30);
        hot_30.setText(Html.fromHtml("热号：红球 <font color=\"#eb4f38\"><b>" + lowNinePlusZero
                (min_1_index) + "," + lowNinePlusZero(min_2_index) + "," + lowNinePlusZero
                (min_3_index) + "," + lowNinePlusZero
                (min_4_index) + "</b></font>| 篮球 <font color=\"#56abe4\"> <b>" + lowNinePlusZero
                (min_1_blue_index) + "," + lowNinePlusZero
                (min_2_blue_index) + "</b></font>"));
        cold_30.setText(Html.fromHtml("冷号：红球 <font color=\"#eb4f38\"><b>" + lowNinePlusZero
                (max_1_index) + "," + lowNinePlusZero(max_2_index) + "," + lowNinePlusZero
                (max_3_index) + "," + lowNinePlusZero
                (max_4_index) + "</b></font>| 篮球 <font color=\"#56abe4\"><b> " + lowNinePlusZero
                (max_1_blue_index) + "," + lowNinePlusZero
                (max_2_blue_index) + "</b></font>"));

        coldhotCompute(50);
        hot_50.setText(Html.fromHtml("热号：红球 <font color=\"#eb4f38\"><b>" + lowNinePlusZero
                (min_1_index) + "," + lowNinePlusZero(min_2_index) + "," + lowNinePlusZero
                (min_3_index) + "," + lowNinePlusZero
                (min_4_index) + "</b></font>| 篮球 <font color=\"#56abe4\"> <b>" + lowNinePlusZero
                (min_1_blue_index) + "," + lowNinePlusZero
                (min_2_blue_index) + "</b></font>"));
        cold_50.setText(Html.fromHtml("冷号：红球 <font color=\"#eb4f38\"><b>" + lowNinePlusZero
                (max_1_index) + "," + lowNinePlusZero(max_2_index) + "," + lowNinePlusZero
                (max_3_index) + "," + lowNinePlusZero
                (max_4_index) + "</b></font>| 篮球 <font color=\"#56abe4\"> <b>" + lowNinePlusZero
                (max_1_blue_index) + "," + lowNinePlusZero
                (max_2_blue_index) + "</b></font>"));

    }


    private String lowNinePlusZero(int z) {
        if (z < 10) {
            return "0" + z;
        } else {
            return String.valueOf(z);
        }
    }

    @Override
    protected void onFailure(int requestCode) {

    }


    private void requestChart() {
        RequestParams params = new RequestParams(UrlConstants.TEST);
        params.addBodyParameter("lotteryType", String.valueOf(LotteryIDUtils.DLT_LOTTERY_ID_INT));
        params.addBodyParameter("page", "1");
        params.addBodyParameter("count", "100");
        httpPost(CommandUtil.TREND_CHART, params, false);

    }


    int min_1_index = 0;
    int min_2_index = 0;
    int min_3_index = 0;
    int min_4_index = 0;
    int max_1_index = 0;
    int max_2_index = 0;
    int max_3_index = 0;
    int max_4_index = 0;

    int min_1_blue_index = 0;
    int min_2_blue_index = 0;
    int min_3_blue_index = 0;
    int max_1_blue_index = 0;
    int max_2_blue_index = 0;
    int max_3_blue_index = 0;

    private void coldhotCompute(int start) {
        if (arrayChartItems == null) {
            return;
        }

        int redNum = 35;
        int blueNum = 12;

        int[] values_100_red = new int[redNum];
        int[] values_100_blue = new int[blueNum];

        for (int i = start; i < arrayChartItems.size(); i++) {
            DLTTrendChart chart = arrayChartItems.get(i);
            String[] hongMiss = chart.hongMiss();
            String[] lanMiss = chart.lanMiss();
            for (int j = 0; j < hongMiss.length; j++) {
                values_100_red[j] += Integer.parseInt(hongMiss[j]);
            }
            for (int k = 0; k < lanMiss.length; k++) {
                values_100_blue[k] += Integer.parseInt(lanMiss[k]);
            }
        }


        ArrayList<Integer> maps = new ArrayList<>();
        for (int i = 0; i < values_100_red.length; i++) {
            maps.add(values_100_red[i]);
        }
        ArrayList<Integer> maps_blue = new ArrayList<>();
        for (int i = 0; i < values_100_blue.length; i++) {
            maps_blue.add(values_100_blue[i]);
        }
        Arrays.sort(values_100_red);
        Arrays.sort(values_100_blue);


        int min_1_blue = values_100_blue[0];
        int min_2_blue = values_100_blue[1];
        int min_3_blue = values_100_blue[2];

        int max_1_blue = values_100_blue[blueNum - 1];
        int max_2_blue = values_100_blue[blueNum - 2];
        int max_3_blue = values_100_blue[blueNum - 2];

        int min_1 = values_100_red[0];
        int min_2 = values_100_red[1];
        int min_3 = values_100_red[2];
        int min_4 = values_100_red[3];

//        x.Ext.setDebug(true);

        LogUtil.v("冷号：" + min_1 + "," + min_2 + "," + min_3);

        int max_1 = values_100_red[redNum - 1];
        int max_2 = values_100_red[redNum - 2];
        int max_3 = values_100_red[redNum - 3];
        int max_4 = values_100_red[redNum - 4];
        LogUtil.v("热号：" + max_1 + "," + max_2 + "," + max_3);


        for (int i = 0; i < maps.size(); i++) {

            if (maps.get(i) == min_1) {
                min_1_index = i + 1;
            }
            if (maps.get(i) == min_2) {
                min_2_index = i + 1;
            }
            if (maps.get(i) == min_3) {
                min_3_index = i + 1;
            }
            if (maps.get(i) == min_4) {
                min_4_index = i + 1;
            }
            if (maps.get(i) == max_1) {
                max_1_index = i + 1;
            }
            if (maps.get(i) == max_2) {
                max_2_index = i + 1;
            }
            if (maps.get(i) == max_3) {
                max_3_index = i + 1;
            }
            if (maps.get(i) == max_4) {
                max_4_index = i + 1;
            }
        }
        for (int i = 0; i < maps_blue.size(); i++) {

            if (maps_blue.get(i) == min_1_blue) {
                min_1_blue_index = i + 1;
            }
            if (maps_blue.get(i) == min_2_blue) {
                min_2_blue_index = i + 1;
            }
            if (maps_blue.get(i) == min_3_blue) {
                min_3_blue_index = i + 1;
            }
            if (maps_blue.get(i) == max_1_blue) {
                max_1_blue_index = i + 1;
            }
            if (maps_blue.get(i) == max_2_blue) {
                max_2_blue_index = i + 1;
            }
            if (maps_blue.get(i) == max_3_blue) {
                max_3_blue_index = i + 1;
            }
        }


    }


    @Override
    public void ballClick() {
        ballClick(null);
    }

    @Override
    public void ballClick(WeiZhi wz) {
        zhushu = (int) new DLT_ZhuShu().betZhuShu(arr_infos);
        tv_zhushu.setText(zhushu + "注");
        tv_money.setText(zhushu * 2 + "米");
    }
}
